package songgao.papers.iEC;


import java.util.Date;
import java.util.LinkedList;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Slave {

	public Slave(){
		this.tracedFinishedGenerationsCounts=new LinkedList<Integer>();
	}
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String encodedKey;
	
	@Persistent
	private Master master;

	@Persistent(defaultFetchGroup="true")
	private LinkedList<Integer> tracedFinishedGenerationsCounts;

	@Persistent
    private String populationKey;
	
	@Persistent
	private Date startingTime;
	
	/**
	 * @param encodedKey
	 *            the encodedKey to set
	 */
	public void setEncodedKey(String encodedKey) {
		this.encodedKey = encodedKey;
	}

	/**
	 * @return the encodedKey
	 */
	public String getEncodedKey() {
		return encodedKey;
	}

	public void pushNewTracedWorkFinishedGenerations(int generation) {
		this.tracedFinishedGenerationsCounts.addLast(generation);
		while (this.tracedFinishedGenerationsCounts.size() > DataManager.INSTANCE.getParameters().getSlavePerformanceTracingDepth())
			this.tracedFinishedGenerationsCounts.removeFirst();
	}

	public int getPerformanceMeasureByTracedWorkFinishedGenerations() {
		int r = 1;
		for (Integer i : this.tracedFinishedGenerationsCounts)
			r += i;
		return r;
	}

	/**
	 * @param startingTime the startingTime to set
	 */
	public void setStartingTime(Date startingTime) {
		this.startingTime = startingTime;
	}

	/**
	 * @return the startingTime
	 */
	public Date getStartingTime() {
		return startingTime;
	}

	/**
	 * @param populationKey the populationKey to set
	 */
	public void setPopulationKey(String populationKey) {
		this.populationKey = populationKey;
	}

	/**
	 * @return the populationKey
	 */
	public String getPopulationKey() {
		return populationKey;
	}

	/**
	 * @param master the master to set
	 */
	public void setMaster(Master master) {
		this.master = master;
	}

	/**
	 * @return the master
	 */
	public Master getMaster() {
		return master;
	}
}
